'use strict';

const Controller = require('egg').Controller;

class UsersController extends Controller {
  //get
  async index() {
    const { ctx, service } = this;
    let params = {
      page:parseInt(ctx.request.query.page),
      page_size:parseInt(ctx.request.query.page_size)
    }
    let users  = await service.users.findAndCountAll(params);
    this.ctx.body = {
        data:users,
        code:1,
        message:'success'
    }
  }

  async create() {
    const { ctx, service } = this;
    let params = ctx.request.body;
    let user = await service.users.findOne({ where:{ tel:params.tel } });
    if(user){
      ctx.fail('手机号已被注册')
      return
    }
    let data = await service.users.create(params);
    ctx.success(data,'注册成功！')
  }
}

module.exports = UsersController;
